"use strict";
Array.prototype.upset_num=function(num) //打乱数组的顺序
{
	if(this.length < 2 || num < 2){return;}
	let loop = Math.ceil((num/this.length));
	let array = []; //存储拼接的数组
	let a = 0;//存取随机数
	for (let j=0;j<loop;j++){
	    for (let i=0;i<this.length;i++){
			a = Math.floor(this.length * Math.random());
		    this.push(this[a]);
		    this.splice(a ,1);
		}
		[...array] = [...array, ...this];
	}
	array.splice(num ,this.length - num%this.length);
	return array;
}

const praise = [
	"做得好！",
	"了不起！",
	"太好了！",
	"恭喜你！",
	"好棒啊！",
	"太完美了！",
	"进步很快！",
	"你很能干！",
	"很突出噢！",
	"很不错嘛！",
	"做得好极了！",
	"你学得真快！",
	"极佳的表现！",
	"你做得很对！",
	"好！",
	"你一定练习过了！",
	"我就知道你能做到的！",
	"你今天做的好认真！",
	"你每天都有进步！",
	"对了，就是这么做！",
	"你像个大师一样！",
	"我想你现在已经做到了！",
	"继续做，你已经有进步了。"
];

//训练软件的操作对象
let train = {

	question_num:0, //问题的数目	
	answer_num: 0, //答题的数目
	score: 0, //得分
	answer: '',//答案
	chapter:'',//章节值,
	topic1:0,//随机表扬语序号
	finish_swith:false,//题目完成开关
	test:[],//要测试的数组
	test_one:[],//要测试的1条数组

	//问题、输入框重置
	reset: function () {
		// console.log(this.test)
		this.test_one = this.test.shift();//获取一题测试题
		$("#question_auto").text(this.test_one[0]); //重置问题
		this.answer = this.test_one[1]; //获取答案
		$("#answer_write").val(''); //输入内容置空
	},

	//按下启动按钮
	begin: function () {
	this.finish_swith = false; //关闭题目完成开关
	$("#question_num").text(this.question_num = 20); //重置题目数
	$("#answer_num").text(this.answer_num = 0); //重置答题数
	$("#score").text(this.score = 0); //重置得分数
	$("#title").text( $("#training_chapter option:checked").text());//获取标题
	this.chapter = $("#training_chapter").val(); //获取章节值
	[...this.test] = [...train_css[this.chapter].upset_num(this.question_num)];
	this.reset();//问题、输入框重置
	$("#begin").attr("disabled",true);//启动按钮无效
	$("#training_chapter").attr("disabled",true);//章节选择框无效
	// console.log('zhengshiming')
	} ,

	//按下提交按钮
	next: function () {
		//开始就按提交或无输出或题目已完成，只能返回
		if (this.chapter== '' || $("#answer_write").val() == '' || this.finish_swith) {return; } 
		this.topic1 = Math.floor(praise.length * Math.random());//获取表扬语随机题目编号
		if (this.answer == answer_write.value) {
			//如果答对，则给提示框成功的背景色
		 		if ($("#message_box").hasClass("alert-danger")) {
		 			$("#message_box").removeClass("alert-danger").addClass("alert-success");
		 		}
			$("#message").text(praise[this.topic1]);//输入表扬的话
			$("#score").text(++this.score);//得分加1
			}
		 else{ //如果答错，则给提示框危险的背景色
		 		if ($("#message_box").hasClass("alert-success")) {
		 			$("#message_box").removeClass("alert-success").addClass("alert-danger");
		 		}
				$("#message").text(this.answer); //输入答案
			}
		$("#answer_num").text(++this.answer_num);//答题数加1
		if(this.question_num == this.answer_num){ //如果答题达到题目数
			alert('完成答题！');
			this.finish_swith = true;
			$("#begin").attr("disabled",false);//启动按钮有效
			$("#training_chapter").attr("disabled",false);//章节选择框有效
			return; //退出程序
		}
		this.reset();//问题、输入框重置
	},

	//按回车键就相当于按提交
	next_key: function (event) { 	
		if (event.keyCode == 13){ 
			this.next()
		};
	}
}